package edu.jsnu.purchase.ticket.controller.ticket;

import dto.resDto.JsonData;
import edu.jsnu.purchase.ticket.controller.common.AbstractController;
import edu.jsnu.purchase.ticket.service.FilmService;
import edu.jsnu.purchase.ticket.service.ScheduleService;
import edu.jsnu.service.ticket.film.dto.FilmDto;
import edu.jsnu.service.ticket.schedule.dto.ScheduleBrief;
import edu.jsnu.service.ticket.schedule.dto.ScheduleShow;
import edu.jsnu.service.ticket.seat.dto.SeatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import util.DataUtils;

import java.util.List;

@Controller
@RequestMapping(value = "ticket/schedule")
public class ScheduleController extends AbstractController {

    @Autowired
    private FilmService filmService;

    @Autowired
    private ScheduleService scheduleService;

    @RequestMapping(value = "schedule.page")
    public String schedulePage(Model model,
                               @RequestParam(value = "filmId")Integer filmId,
                               @RequestParam(value = "flag")Integer flag){
        FilmDto film = filmService.getFilmById(filmId);
        model.addAttribute("film", film);
        List<String> dateList = DataUtils.getFuture6Days();
        model.addAttribute("dateList", dateList);
        model.addAttribute("flag", flag);
        return "ticket/schedule";
    }

    /**
     * 场次信息
     * @param filmId
     * @param date
     * @return
     */
    @RequestMapping(value = "schedule.do", method = RequestMethod.GET)
    @ResponseBody
    public JsonData scheduleData(@RequestParam (value = "filmId")Integer filmId,
                                 @RequestParam(value = "date")String date){
        try{
            List<ScheduleBrief> list = scheduleService.getBriefSchedule(filmId, date);
            return JsonData.success(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail("查询场次信息异常，请联系管理员");
    }

    /**
     * 选座页面
     * @param model
     * @param scheduleId
     * @return
     */
    @RequestMapping("seat.page")
    public String seatPage(Model model, @RequestParam(value = "scheduleId") Integer scheduleId){
        //场次信息
        ScheduleShow schedule = scheduleService.getScheduleInfo(scheduleId);
        model.addAttribute("schedule", schedule);
        //座位信息
        List<List<SeatVo>> seatList = scheduleService.getSeatInfo(scheduleId);
        model.addAttribute("seatList", seatList);
        return "ticket/seat";
    }

}
